<script type="text/javascript">
function populateCats(sourceId, targetId)
{
	$("#"+targetId).val(0);
	$.getJSON("/commander/json.php?con=6&list=cat", { typeId: $("#"+sourceId).val(), ajax: 'true' }, function(j)
	{
		  var opts = '';
		  if(j)
		  {
			  for (var i = 0; i < j.length; i++) 
			  { opts += '<option value="' + j[i].id + '">' + j[i].title + '</option>'; }
		  }
		  else
		  { opts += '<option value="0"><?php echo ucfirst($txt->getWord('undefined', $words)); ?></option>'; }
		  
		  $("#"+targetId).html(opts);
	})
	<?php if($adc->getSetting('NewAdBrands') > 0): ?>
	populateBrands(sourceId, targetId, 'new_BrandId');
	<?php endif; ?>
	$(".IsBuyingButtons button").removeClass('active');
	populateIsBuying(sourceId, targetId);
}

function populateBrands(sourceId, source2Id, targetId)
{
	$.getJSON("/commander/json.php?con=6&list=adbrand", { typeId: $("#"+sourceId).val(), catId: $("#"+source2Id).val(), ajax: 'true' }, function(j)
	{
		  var opts = '';
		  if(j)
		  {
			  for (var i = 0; i < j.length; i++) 
			  { opts += '<option value="' + j[i].id + '">' + j[i].title + '</option>'; }
		  }
		  else
		  { opts += '<option value="0"><?php echo ucfirst($txt->getWord('undefined')); ?></option>'; }
		  $("#"+targetId).html(opts);
	})
}

function populateModels(sourceId, targetId)
{
	$("#"+targetId).val(0);
	$.getJSON("/commander/json.php?con=6&list=model", { brandId: $("#"+sourceId).val(), ajax: 'true' }, function(j)
	{
		  var opts = '';
		  if(j)
		  {
			  for (var i = 0; i < j.length; i++) 
			  { opts += '<option value="' + j[i].id + '">' + j[i].title + '</option>'; }
		  }
		  else
		  { opts += '<option value="0"><?php echo ucfirst($txt->getWord('undefined', $words)); ?></option>'; }
		  
		  $("#"+targetId).html(opts);
	});
}

function populateIsBuying(sourceId, sourceId2)
{
	var valType = 0;
	var valCat = 0;
	
	valType = $("#"+sourceId).val();
	valCat  = $("#"+sourceId2).val();
	
	$(".IsBuyingButtons").css('display', 'none');
	
	if((valType > 0) && (valCat > 0))
	{
		if(valType < 5)
		{ 
			$(".IsBuying1").css('display', 'block'); 
			$(".IsBuying1").css('visibility', 'visible');
		}
		if(valType == 5)
		{ 
			$(".IsBuying5").css('display', 'block'); 
			$(".IsBuying5").css('visibility', 'visible');
		}
		if(valType == 6)
		{ 
			$(".IsBuying6").css('display', 'block');
			$(".IsBuying6").css('visibility', 'visible');
		}
		return 1;
	}
	else
	{
		$("#proceedButton").removeClass('in');
		$(".GalleryDisplay").css('display', 'none');
		$(".AdQuotas").css('display', 'none');
		return 0;
	}
}

function checkProceed(valType, buyType)
{
	if((valType > 0) && (buyType > 0))
	{
		
		if(valType < 5)
		{
			if(buyType > 2)
			{ $("#IsRent").val(1); }
			else
			{ $("#IsRent").val(0); }
			
			if((buyType == 2) || (buyType == 4))
			{ 
				$("#IsBuying").val(1);
				$("#UpgradeCheck").val(0);
				$(".infoUpgrade").css('display', 'none');
				$(".Upgrade").css('display', 'none');
				$(".GalleryDisplay").css('display', 'none');
				$(".AdQuotas").css('display', 'none');
			}
			else
			{ 
				$("#IsBuying").val(0); 
				$(".GalleryDisplay").show('blind');
				$(".AdQuotas").show('blind');
				$("#UpgradeCheck").val(1);
				$(".infoUpgrade").show('blind');
				$(".Upgrade").show('blind');
			}
			if(buyType > 1)
			{ 
				$(".GalleryDisplay").css('display', 'none');
				$(".AdQuotas").css('display', 'none'); 
			}
		}
		else
		{
			$("#UpgradeCheck").val(0);
			$(".infoUpgrade").css('display', 'none');
			$(".Upgrade").css('display', 'none');
			$(".GalleryDisplay").css('display', 'none');
			$(".AdQuotas").css('display', 'none');
			
			$("#IsRent").val(0);
			if(buyType > 1)
			{ $("#IsBuying").val(1); }
			else
			{ $("#IsBuying").val(0); }
		}
		$("#proceedButton").addClass('in');
		$(".progress .bar").css('width', '100%');
	}
	else
	{ 
		$("#proceedButton").removeClass('in');
		$(".GalleryDisplay").css('display', 'none');
		$(".AdQuotas").css('display', 'none');
	}
}

function togglePrice(display)
{
	if(display == 1)
	{ $("#editor_ad_price_fields" ).show('highlight', {}, 500); }
	if(display == 0)
	{ 
		$("#editor_ad_price_fields" ).hide('highlight', {}, 500);
		if($("#priceLocal").val() == '')
		{ 
			$("#priceLocal").val(0);
			$("#priceGlobal").val(0);
		}
		if($("#priceGlobal").val() == '')
		{ 
			$("#priceLocal").val(0);
			$("#priceGlobal").val(0);
		}
	}
}


function checkAdTitle()
{
	<?php if(is_object($adc->getCurrentAd())): ?>
		var title = $("#AdTitle").val();
		var brand = '';
		var model = '';
		var year  = '';
		
		<?php if ($adc->getCurrentAd()->getUseBrand()): ?>
			brand = $("#AdBrand option:selected").text();
			if(brand == '-') { brand = ''; }
		<?php endif; ?>
		
		<?php if ($adc->getCurrentAd()->getUseModel()): ?>
			<?php if($adc->getSetting('model-manager') > 0): ?>
			model = $("#AdModel option:selected").text();
			<?php else: ?>
			model = $("#AdModel").val();
			<?php endif; ?>
		<?php endif; ?>
		
		<?php if ($adc->getCurrentAd()->getUseYear()): ?>
		year  = $("#AdYear option:selected").text();
			<?php if($adc->getCurrentAd()->getIsBuying()): ?>
			year  = '<?php echo ucfirst($txt->getWord('from')); ?> ' + year;
			<?php endif; ?>
		<?php endif; ?>
		
		<?php if($adc->getCurrentAd()->getTypeId() < 4): ?>
			$("#AdTitle").val($.trim(brand + ' ' + model + ' ' + year));
		<?php else: ?>
			$("#AdTitle").val($.trim(brand + ' ' + model));
		<?php endif; ?>
	<?php endif; ?>
}

function setAdContact()
{
	var contacts = new Array();
	<?php 
	if($user->getUserContacts())
	{
		?>
		contacts[0] = new Array();
		contacts[0]['ContactEmail'] = '<?php echo $controller->getForm('editor_ad_contact')->getField('ContactEmail')->getValue(); ?>';
		contacts[0]['ContactName'] 	= '<?php echo $controller->getForm('editor_ad_contact')->getField('ContactName')->getValue(); ?>';
		contacts[0]['ContactLastName'] 	= '';
		contacts[0]['ContactPhone'] = '<?php echo $controller->getForm('editor_ad_contact')->getField('ContactPhone')->getValue(); ?>';
		<?php
		foreach($user->getUserContacts() as $contact)
		{
			?>
			contacts[<?php echo $contact->getId(); ?>] = new Array();
			contacts[<?php echo $contact->getId(); ?>]['ContactEmail'] 	= '<?php echo $contact->getEmail(); ?>';
			contacts[<?php echo $contact->getId(); ?>]['ContactName'] 	= '<?php echo $contact->getName(); ?>';
			contacts[<?php echo $contact->getId(); ?>]['ContactLastName'] = '<?php echo $contact->getLastName(); ?>';
			contacts[<?php echo $contact->getId(); ?>]['ContactPhone'] 	= '<?php echo $contact->getPhone(); ?>';
			<?php
		}
	}
	?>
	var setConId = $('#ContactId option:selected').val();
	if(setConId > -1)
	{
		$('#ContactEmail').val(contacts[setConId]['ContactEmail']);
		$('#ContactEmail').effect('highlight', {}, 500);
		
		$('#ContactName').val(contacts[setConId]['ContactName'] + ' ' + contacts[setConId]['ContactLastName']);
		$('#ContactName').effect('highlight', {}, 500);
		
		$('#ContactMob').val(contacts[setConId]['ContactPhone']);
		$('#ContactMob').effect('highlight', {}, 500);
	}
}

function setAdLocation()
{
	var locations = new Array();
	<?php 
	if($user->getUserLocations())
	{
		?>
		locations[0] = new Array();
		locations[0]['LocationAddress'] = '<?php echo $controller->getForm('editor_ad_location')->getField('LocationAddress')->getValue(); ?>';
		locations[0]['LocationZip'] 	= '<?php echo $controller->getForm('editor_ad_location')->getField('LocationZip')->getValue(); ?>';
		locations[0]['LocationCity'] = '<?php echo $controller->getForm('editor_ad_location')->getField('LocationCity')->getValue(); ?>';
		locations[0]['LocationCountry'] = '<?php echo $controller->getForm('editor_ad_location')->getField('LocationCountry')->getValue(); ?>';
		<?php
		foreach($user->getUserLocations() as $location)
		{
			?>
			locations[<?php echo $location->getId(); ?>] = new Array();
			locations[<?php echo $location->getId(); ?>]['LocationAddress'] 	= '<?php echo $location->getAddress(); ?>';
			locations[<?php echo $location->getId(); ?>]['LocationZip'] 	= '<?php echo $location->getZip(); ?>';
			locations[<?php echo $location->getId(); ?>]['LocationCity'] 	= '<?php echo $location->getCity(); ?>';
			locations[<?php echo $location->getId(); ?>]['LocationCountry'] 	= '<?php echo $location->getCountry(); ?>';
			<?php
		}
	}
	?>
	var setConId = $('#LocationId option:selected').val();
	if(setConId > -1)
	{
		$('#LocationAddress').val(locations[setConId]['LocationAddress']);
		$('#LocationAddress').effect('highlight', {}, 500);
		
		$('#LocationZip').val(locations[setConId]['LocationZip']);
		$('#LocationZip').effect('highlight', {}, 500);
		
		$('#LocationCity').val(locations[setConId]['LocationCity']);
		$('#LocationCity').effect('highlight', {}, 500);
		
		$('#LocationCountry').val(locations[setConId]['LocationCountry']);
		$('#LocationCountry').effect('highlight', {}, 500);
	}
}

function convertFromCurrency(fromIsoSelectId, sourceId, targetId)
{
	var currencyTable = new Array();
	var ammount = $("#"+sourceId).val();
	var fromIso = $("#"+fromIsoSelectId+' option:selected').val();
	var convVal = 0;
	
	<?php 
	$currencies = $shop->getCurrencies();
	foreach($currencies as $cur)
	{
		?>
		currencyTable['<?php echo $cur['code']; ?>'] = new Array();
		currencyTable['<?php echo $cur['code']; ?>']['unit'] = <?php echo $cur['unit']; ?>;
		currencyTable['<?php echo $cur['code']; ?>']['value'] = <?php echo $cur['value']; ?>;
		<?php
	}
	?>
	if((ammount > 0) && (fromIso != ''))
	{
		var curUnit = currencyTable[fromIso]['unit'];
		var curVal 	= currencyTable[fromIso]['value'];
		
		if(curUnit > 0)
		{
			convVal = (curVal / curUnit) * ammount;
			var finalVal = new Number(convVal);
			$("#"+targetId).val(finalVal.toFixed(2));
		}
	}
}

function convertToCurrency(fromIsoSelectId, sourceId, targetId)
{
	var currencyTable = new Array();
	var ammount = $("#"+sourceId).val();
	var fromIso = $("#"+fromIsoSelectId+' option:selected').val();
	convVal =0;
	
	<?php 
	$currencies = $shop->getCurrencies();
	foreach($currencies as $cur)
	{
		?>
		currencyTable['<?php echo $cur['code']; ?>'] = new Array();
		currencyTable['<?php echo $cur['code']; ?>']['unit'] = <?php echo $cur['unit']; ?>;
		currencyTable['<?php echo $cur['code']; ?>']['value'] = <?php echo $cur['value']; ?>;
		<?php
	}
	?>
	if((ammount > 0) && (fromIso != ''))
	{
		var curUnit = currencyTable[fromIso]['unit'];
		var curVal 	= currencyTable[fromIso]['value'];
		
		if(curUnit > 0)
		{
			convVal = (ammount / curVal) * curUnit;
			var finalVal = new Number(convVal);
			$("#"+targetId).val(finalVal.toFixed(2));
		}
	}
}

$(document).ready(function() 
{ 
	$(".accordion").collapse();
	
	$("#IsBuying").val(0);
	$("#IsRent").val(0);
	
	$(".IsBuyingButtons").css('display', 'none');
	$(".IsBuyingButtons").css('visibility', 'hidden');
	
	$(".infoUpgrade").css('display', 'none');
	$(".Upgrade").css('display', 'none');
	$("#UpgradeCheck").val(0);
	
	$(".IsBuying1 button").click(function () 
	{ checkProceed(1, $(this).val()); });
	$(".IsBuying5 button").click(function () 
	{ 
		checkProceed(5, $(this).val()); 
		$("#UpgradeCheck").val(0);
		$(".infoUpgrade").css('display', 'none');
		$(".Upgrade").css('display', 'none');
	});
	
	$("#additional-fields input.numeric").click(function () 
	{  $(this).val(''); });

	$(".IsBuying6 button").click(function () 
	{ 
		//Stillingsannonser
		checkProceed(6, $(this).val()); 
		$("#UpgradeCheck").val(0);
		$(".infoUpgrade").css('display', 'none');
		$(".Upgrade").css('display', 'none');
	});
	
	$(".date_pick").datepicker(
	{	
		dateFormat: 'dd.mm.yy',
		maxDate: 0,
		changeMonth: true,
		changeYear: true,
		yearRange: "1900:<?php echo date('Y'); ?>"
	});
	
	$(".date_pick_from").attr("id", "from");
	$(".date_pick_to").attr("id", "to");
	
	var dates = $( "#from, #to" ).datepicker({
		changeMonth: true,
		changeYear: true,
		onSelect: function( selectedDate ) {
			var option = this.id == "from" ? "minDate" : "maxDate",
				instance = $( this ).data( "datepicker" ),
				date = $.datepicker.parseDate(
					instance.settings.dateFormat ||
					$.datepicker._defaults.dateFormat,
					selectedDate, instance.settings );
			dates.not( this ).datepicker( "option", option, date );
		}
	});
	
	<?php if($adc->getSetting('NewAdWizard') > 0): ?>
		<?php if(intval($_GET['typeId']) > 0): ?>
			$(".progress .bar").css('width', '25%');
			<?php if(intval($_GET['catId']) > 0): ?>
				$(".progress .bar").css('width', '50%');
				<?php if(intval($_GET['brandId']) > 0): ?>
					$(".progress .bar").css('width', '75%');
					populateIsBuying('new_TypeId', 'new_CatId');
				<?php else: ?>
					populateBrands('new_TypeId', 'new_CatId', 'new_BrandId');
				<?php endif; ?>
			<?php endif; ?>
		<?php endif; ?>
	<?php endif; ?>
	
	
	
	<?php if($_POST): ?>
	if($.cookie("scroll") > 0)
	{ document.documentElement.scrollTop = $.cookie("scroll") || 0; }
	<?php endif; ?>
	
	<?php if($_POST['preview'] == '1'): ?>
	window.location = "<?php echo SITE_URL; ?>ad/<?php echo $adc->getCurrentAd()->getId(); ?>/";
	<?php endif; ?>
})
</script>